package net.realsee.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.domain.system.bo.SysLogFileBo;
import net.realsee.domain.system.vo.SysLogFileVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.service.system.ISysLogFileService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 日志文件 操作处理
 *
 * @author realsee
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/log/file")
public class SysLogFileController extends BaseController {

    private final ISysLogFileService logFileService;

    /**
     * 获取日志文件列表
     */
    @SaCheckPermission("system:logFile:list")
    @GetMapping("/list")
    public TableDataInfo<SysLogFileVo> list(SysLogFileBo param, PageQuery pageQuery) {
        return logFileService.list(param, pageQuery);
    }

    /**
     * 查询日志文件尾行内容
     */
    @SaCheckPermission("system:logFile:view")
    @GetMapping("/tail")
    public R<Map<String, String>> tail(SysLogFileBo param) {
        return R.ok(logFileService.tail(param));
    }

    /**
     * 下载日志文件
     */
    @SaCheckPermission("system:logFile:download")
    @GetMapping("/download")
    public void download(SysLogFileBo param, HttpServletResponse response) throws IOException {
        logFileService.download(param, response);
    }

    /**
     * 删除日志文件
     **/
    @SaCheckPermission("system:logFile:delete")
    @DeleteMapping("/delete")
    public R<Void> delete(SysLogFileBo param) {
        return toAjax(logFileService.delete(param.getPath()));
    }

}
